import router from "@/router";
import store from "@/store";
import { getToken } from "@/utils/auth.js";

const whiteList = ["/login"];
router.beforeEach((to, from, next) => {
  if (getToken()) {
    if (to.path === "/login") {
      next({ path: "/" });
    } else {
      if (store.getters.roles.length === 0) {
        // 判断当前用户是否已拉取完user_info信息
        store
          .dispatch("GetInfo")
          .then(() => {
            next();
          })
          .catch((err) => {
            store.dispatch("LogOut").then(() => {
              Message.error(err);
              next({ path: "/" });
            });
          });
      } else {
        next();
      }
    }
    next();
  } else {
    // if (whiteList.indexOf(to.path) > -1) {
    //   next();
    // } else {
    //   next("/login");
    // }
    next();
  }
});
